package com.sinaapp.kltq.dinosaurweather;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.util.List;
import java.util.Map;

/**
 * Created by 丰小兄 on 2015/7/27.
 */
public class MySimpleAdapter extends SimpleAdapter {

    private Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (currentBtn != null) {
                        // 替换为黑色按钮
                        ((ImageView) currentBtn).setImageResource(R.drawable.btn_remove);
                        // 旋转图片
                        ((ImageView) currentBtn).setRotation(0f);
                        // 旋转动画
                        Animation operatingAnim = AnimationUtils.loadAnimation(mContext, R.anim.rotate_remove_back);
                        LinearInterpolator lin = new LinearInterpolator();
                        operatingAnim.setInterpolator(lin);
                        if (operatingAnim != null) {
                            currentBtn.startAnimation(operatingAnim);
                        }
                        // 设置tag
                        currentBtn.setTag(-1 * (int) currentBtn.getTag());
                        // 解锁
                        lock = false;
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private Context mContext;
    private View currentBtn;
    private boolean lock;
    private List<? extends Map<String, ?>> mData;

    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        mContext = context;
        mData = data;
        lock = false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);

        ImageView btnRemove = (ImageView) v.findViewById(R.id.btnRemove);
        btnRemove.setTag(position + 1);
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((int) v.getTag() > 0 && !lock) { // 如果未加锁且目标未旋转
                    // 替换为红色按钮
                    ((ImageView) v).setImageResource(R.drawable.btn_remove_r);
                    // 旋转图片
                    ((ImageView) v).setRotation(-90f);
                    // 旋转动画
                    Animation operatingAnim = AnimationUtils.loadAnimation(mContext, R.anim.rotate_remove);
                    LinearInterpolator lin = new LinearInterpolator();
                    operatingAnim.setInterpolator(lin);
                    if (operatingAnim != null) {
                        v.startAnimation(operatingAnim);
                    }
                    // 设置tag
                    v.setTag(-1 * (int) v.getTag());
                    // 锁
                    lock = true;
                    // 3秒不点击恢复
                    currentBtn = v;
                    new Handler().postDelayed(new Runnable() {
                        public void run() {

                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    Message message = new Message();
                                    message.what = 1;
                                    myHandler.sendMessage(message);
                                }
                            }).start();
                        }
                    }, 3000);
                } else if ((int) v.getTag() < 0) {  // 如果目标已旋转
                    int position = ((int) v.getTag() * (-1) - 1);
                    // 删除城市
                    WeatherManager wm = new WeatherManager(mContext);
                    wm.delCityFromList(wm.getCityList().get(position).city);
                    // 删除条目
                    mData.remove(((int) v.getTag() * (-1) - 1));
                    MySimpleAdapter.this.notifyDataSetChanged();
                    // 替换为黑色按钮
                    ((ImageView) currentBtn).setImageResource(R.drawable.btn_remove);
                    // 旋转图片
                    ((ImageView) currentBtn).setRotation(0f);
                    // 设置当前按钮
                    currentBtn = null;
                    // 解锁
                    lock = false;
                }
            }
        });

        return v;
    }
}
